package com.centaline.androidsalesblog.base;

import android.net.Uri;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.BitmapTypeRequest;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.centaline.androidsalesblog.R;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.glide.LogListener;
import com.centanet.framework.glide.RoundImageViewTarget;

/**
 * 聊天基类
 * Created by guilin on 16/3/16.
 */
public abstract class BaseChatVH extends RecyclerView.ViewHolder {

    public AppCompatTextView atv_date;
    public ImageView img_user;

    public BaseChatVH(View itemView) {
        super(itemView);
        atv_date = (AppCompatTextView) itemView.findViewById(R.id.atv_date);
        img_user = (ImageView) itemView.findViewById(R.id.img_user);
    }

    /**
     * 时间显示
     */
    public void msgDate(String date, boolean show) {
        if (show) {
            atv_date.setVisibility(View.VISIBLE);
            atv_date.setText(date);
        } else {
            atv_date.setVisibility(View.GONE);
            atv_date.setText(null);
        }
    }

    /**
     * 用户头像
     */
    public void userImg(BitmapTypeRequest<String> mBitmapTypeRequest, String url) {
        if (GlideLoad.needLoad(true)) {
            mBitmapTypeRequest
                    .load(url)
                    .placeholder(R.drawable.ic_staff_default)
                    .error(R.drawable.ic_staff_default)
                    .centerCrop()
                    .into(new RoundImageViewTarget(img_user));
        } else {
            mBitmapTypeRequest
                    .load("")
                    .placeholder(R.drawable.ic_staff_default)
                    .error(R.drawable.ic_staff_default)
                    .centerCrop()
                    .into(new RoundImageViewTarget(img_user));
        }
    }

    /**
     * 消息文本
     */
    public void msgContent(AppCompatTextView textView, CharSequence text) {
        textView.setText(text);
    }

    /**
     * 消息图片
     *
     * @param path      原图
     * @param thumbnail 缩略图
     */
    public void msgImg(ImageView imageView, DrawableRequestBuilder<Uri> requestBuilder, Uri path, Uri thumbnail) {
        if (GlideLoad.needLoad(true)) {
            DrawableRequestBuilder<Uri> thumbnailRequest = requestBuilder.clone();
            thumbnailRequest.load(thumbnail);
            requestBuilder
                    .load(path)
                    .listener(new LogListener<Uri, GlideDrawable>())
                    .centerCrop()
                    .thumbnail(thumbnailRequest)
                    .into(imageView);
        } else {
            requestBuilder.load(Uri.EMPTY)
                    .error(R.drawable.ic_centa_square_logo)
                    .listener(new LogListener<Uri, GlideDrawable>())
                    .centerCrop()
                    .into(imageView);
        }
    }
}
